package wowmobile;

import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Server extends Thread {
    
    private ExecutorService exec;
    private ServerSocket server;
    private ArrayList<Player> players;
    private Communication c;
    
    public Server()
    {
        players=new ArrayList<Player>();
        exec = Executors.newCachedThreadPool();
    }
    
    public void run() {
        try
        {
            server = new ServerSocket(3456);
        }
        catch(Exception e)
        {
            System.out.println("Serwer constructor:"+e);
        }
        try
        {
            System.out.println("Server is running.");
            while(true)
            {               
                Socket channel = server.accept();
                c = new Communication(channel);
                newPlayer(channel);
            }
        }
        catch (Exception exception)
        {
            System.out.println("Server closed");
            return;
        }
    }
    public void newPlayer(Socket channel){
        try
        {
        String login = c.getLogin();
                if(Database.CheckLogin(login)){
                    c.sendIsLogin(true);
                    Player p = new Player(channel, this,login);
                    players.add(p);
                    System.out.println("New player");
                    exec.execute(p);
                } else {
                    c.sendIsLogin(false);
                    newPlayer(channel);
                }
                }
        catch (Exception exception)
        {
            System.out.println("Server closed");
            return;
        }
    }
    public void killServer()
    {
        try
        {
            server.close();
            for(int i=0; i<players.size(); i++)
                players.get(i).channel.close();
        }
        catch(Exception e)
        {
            System.out.println("Server close:"+e);
        }
    }

    public ArrayList<Coordinate> findPlayersByArea(Area area,Player p)
    {
        ArrayList<Coordinate> tmp=new ArrayList<Coordinate>();
        for(int i=0; i<players.size();i++)
            if(players.get(i).area.getName().equals(area.getName()) && players.get(i)!=p)
                tmp.add(players.get(i).place);
        return tmp;
    }

    public String getPlayers(Area area, Player p)
    {
        String tmp=p.place.toString();
        ArrayList<Coordinate> alc=findPlayersByArea(area,p);
        for(int i=0; i<alc.size();i++)
            tmp=tmp+"[:]1,"+alc.get(i).getX()+","+alc.get(i).getY();
        return tmp;
    }

    void deletePlayer(Player p)
    {
        players.remove(p);
    }

    public Player getPlayerByName(String login) throws Exception
    {
        for(int i=0;i<players.size();i++)
        {
            if(players.get(i).getStats().getNick().equals(login))
                return players.get(i);
        }
        throw new Exception("Zjebales");
    }
}
